package com.example.hyznbk.view.fragment;

import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.Html;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.hyznbk.MyApplication;
import com.example.hyznbk.R;
import com.example.hyznbk.model.result.BaseReviewWrongEntity;
import com.example.hyznbk.model.result.LearnTypeEntity;
import com.example.hyznbk.view.adapter.WrongReviewAdapter;
import com.example.hyznbk.view.adapter.PopupMenuType1Adapter;
import com.example.hyznbk.view.base.BaseMainFragment;
import com.example.hyznbk.view.base.ILoadMoreDataView;
import com.example.hyznbk.view.widget.CustomFooterView;
import com.example.hyznbk.view.widget.PopupMenu;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.example.hyznbk.presenter.BaseReviewWrongPresenter;

import static com.example.hyznbk.view.activity.WrongReviewActivity.WRONG_FROM;

/**
 * Created by tangminglong on 17/8/11.
 */

public class WrongReviewFragment extends BaseMainFragment<BaseReviewWrongPresenter> implements View.OnClickListener,ILoadMoreDataView<BaseReviewWrongEntity> {

    private TextView mTvQuesTotal;
    private TextView mTvSPQuesType;
    private TextView mTvSPQuesStatus;
    private View mViewMasker;
    private RelativeLayout mRelToolHead;
    private Toolbar mToolbar;

    private PopupMenu mQuesTypePopupMenu;
    private PopupMenu mQuesStatusPopupMenu;
    private PopupMenuType1Adapter mQuesTypeAdapter;
    private PopupMenuType1Adapter mQuesStatusAdapter;
    private WrongReviewAdapter mBaseReviewWrongAdapter;


    //private Map<String,String> mQuesTypeMap = new HashMap<>();
    private Map<String,Integer> mQuesStatusMap = new HashMap<>();
    private List<LearnTypeEntity> learnTypeList;
    private Map<String,String> LearnTypeMap = new HashMap<>();

    @Override
    public void initBaseMainViews(View view, Bundle savedInstanceState) {
        learnTypeList = ((MyApplication)getActivity().getApplication()).getLearnTypeEntities();
        int wrongFrom = getHolderActivity().getIntent().getIntExtra(WRONG_FROM, 0);
        mPresenter = new BaseReviewWrongPresenter(this,wrongFrom);
        String title = (wrongFrom == 0 ? getString(R.string.baseReviewWrong) : getString(R.string.difficultReviewWrong));
        mViewMasker = view.findViewById(R.id.view_masker);
        mBaseReviewWrongAdapter = new WrongReviewAdapter(getHolderActivity());
        mBaseReviewWrongAdapter.setCustomLoadMoreView(new CustomFooterView(getActivity()));
        mRecyclerView.setAdapter(mBaseReviewWrongAdapter);
        mTvSPQuesType = (TextView) view.findViewById(R.id.sp_quesType);
        mTvSPQuesStatus = (TextView) view.findViewById(R.id.sp_status);
        mTvQuesTotal = (TextView) view.findViewById(R.id.tv_quesTotal);
        mRelToolHead = (RelativeLayout) view.findViewById(R.id.rel_toolbar_head);
        mToolbar = (Toolbar) view.findViewById(R.id.toolbar);
        //mToolbar.setNavigationIcon(R.mipmap.ic_back);
        initToolBar(mToolbar,true,title);
        mTvSPQuesType.setOnClickListener(this);
        mTvSPQuesStatus.setOnClickListener(this);
        mQuesTypeAdapter = new PopupMenuType1Adapter(getHolderActivity());
        mQuesStatusAdapter = new PopupMenuType1Adapter(getHolderActivity());
        mQuesTypePopupMenu = new PopupMenu(getHolderActivity(), ViewGroup.LayoutParams.MATCH_PARENT,R.drawable.bg_popupmenu);
        mQuesStatusPopupMenu = new PopupMenu(getHolderActivity(), ViewGroup.LayoutParams.MATCH_PARENT,R.drawable.bg_popupmenu);

        String[] quesStatusArrays = getResources().getStringArray(R.array.QuesStatusArrays_Two);
        String[] quesStatusName = new String[quesStatusArrays.length];
        for (int i = 0 ; i < quesStatusArrays.length ; i++){
            String[] split = quesStatusArrays[i].split("\\|");
            quesStatusName[i] = split[0];
            mQuesStatusMap.put(split[0], Integer.valueOf(split[1]));
        }

       /* String[] quesTypeArray = new String[0];
        if(UserInfo.checkStage(UserInfo.JUNIOR_SCHOOL)){
            quesTypeArray = getResources().getStringArray(R.array.QuesType_MiddleSchool_Arrays);
        }else if(UserInfo.checkStage(UserInfo.SENIOR_SCHOOL)){
            quesTypeArray = getResources().getStringArray(R.array.QuesType_HighSchool_Arrays);
        }

        String[] quesTypeName = new String[quesTypeArray.length];
        for (int i = 0 ; i < quesTypeArray.length;i++){
            String[] split = quesTypeArray[i].split("\\|",-1);
            quesTypeName[i] = split[0];
            mQuesTypeMap.put(split[0],split[1]);
        }*/

        String[] learnTypeNames = new String[learnTypeList.size()];

        for (int i = 0 ; i < learnTypeList.size() ; i++){
            LearnTypeEntity learnTypeEntity = learnTypeList.get(i);
            learnTypeNames[i] = learnTypeEntity.getTypeName();
            LearnTypeMap.put(learnTypeEntity.getTypeName(),learnTypeEntity.getTypeID());
        }

        mQuesStatusAdapter = new PopupMenuType1Adapter(getHolderActivity(),quesStatusName);
        mQuesTypeAdapter = new PopupMenuType1Adapter(getHolderActivity(),learnTypeNames);
        mQuesStatusPopupMenu.setListAdapter(mQuesStatusAdapter);
        mQuesTypePopupMenu.setListAdapter(mQuesTypeAdapter);
        mQuesStatusPopupMenu.setCheckIndex(1);
        mTvSPQuesType.setText(mQuesTypePopupMenu.getCheckedStr());
        mTvSPQuesStatus.setText(mQuesStatusPopupMenu.getCheckedStr());
        mQuesTypePopupMenu.setOnItemClickListener(new PopupMenu.OnItemClickListener() {
            @Override
            public void onItemClick(View item, int position, long id) {
                mTvSPQuesType.setText(mQuesTypePopupMenu.getCheckedStr());
                mXRefreshView.startRefresh();
                mPresenter.getDataByQuesType(LearnTypeMap.get(mQuesTypePopupMenu.getCheckedStr()));
            }
        });

        mQuesTypePopupMenu.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                mTvSPQuesType.setSelected(false);
                mViewMasker.setVisibility(View.GONE);
            }
        });

        mQuesStatusPopupMenu.setOnItemClickListener(new PopupMenu.OnItemClickListener() {
            @Override
            public void onItemClick(View item, int position, long id) {
                mTvSPQuesStatus.setText(mQuesStatusPopupMenu.getCheckedStr());
                mXRefreshView.startRefresh();
                mPresenter.getDataByStatus(mQuesStatusMap.get(mQuesStatusPopupMenu.getCheckedStr()));
            }
        });

        mQuesStatusPopupMenu.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                mTvSPQuesStatus.setSelected(false);
                mViewMasker.setVisibility(View.GONE);
            }
        });

        mBaseReviewWrongAdapter.setOnItemClickListener(new WrongReviewAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, List<BaseReviewWrongEntity.ModelListBean> list, int position) {
                mPresenter.openItemDetail(position,list.get(position));
            }
        });

        mBaseReviewWrongAdapter.setOnItemHistoryClickListener(new WrongReviewAdapter.OnItemHistoryClickListener() {
            @Override
            public void onItemClick(View view, List<BaseReviewWrongEntity.ModelListBean> list, int position) {
                mPresenter.openItemHistoryDetail(position,list.get(position));
            }
        });
    }

    @Override
    protected void updateViews(boolean isShowLoading) {
        mPresenter.getData(isShowLoading);
    }

    @Override
    public int attachLayoutRes() {
        return R.layout.fragment_wrong;
    }

    @Override
    public void loadData(BaseReviewWrongEntity data) {
        if(mRecyclerView.getLayoutManager() != null ) {
            mRecyclerView.getViewTreeObserver().addOnGlobalLayoutListener(layoutListener);
        }
        setQuesTotal(data.getTotalCount());
        mBaseReviewWrongAdapter.clearAndAdd(data.getModelList());
        loadComplete(mBaseReviewWrongAdapter.getAdapterItemCount() >= data.getTotalCount());

    }



    @Override
    public void loadMoreData(BaseReviewWrongEntity data) {
        setQuesTotal(data.getTotalCount());
        mBaseReviewWrongAdapter.add(data.getModelList());

        loadComplete(mBaseReviewWrongAdapter.getAdapterItemCount() >= data.getTotalCount());
    }

    @Override
    public void showNetError() {
        super.showNetError();
        setQuesTotal(0);
    }

    @Override
    public void showNoData(String msg) {
        super.showNoData(msg);
        setQuesTotal(0);
    }

    /**
     * 更新题目总数
     * @param totalCount
     */
    private void setQuesTotal(int totalCount) {
        mTvQuesTotal.setText(Html.fromHtml(String.format(getString(R.string.quesTotal1),totalCount)));
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.sp_quesType:
                mViewMasker.setVisibility(View.VISIBLE);
                mQuesTypePopupMenu.showLocation(mRelToolHead,0,0);
                mTvSPQuesType.setSelected(true);
                break;
            case R.id.sp_status:
                mViewMasker.setVisibility(View.VISIBLE);
                mQuesStatusPopupMenu.showLocation(mRelToolHead,0,0);
                mTvSPQuesStatus.setSelected(true);
                break;
        }
    }
}
